﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
using System.Web;
using System.IO;

namespace Core.DS
{
    public static partial class CourseDesign
    {
        public static dynamic submitCd(string username, string subjects,HttpPostedFileBase userfile, string ext)
        {
            var sql = new Sql.SqlDataContext();
            var u = sql.User.FirstOrDefault(row => row.username == username);
            if (u == null)
            {
                return new
                {
                    success = false,
                    msg = "User不存在"
                };
            }
            var s = sql.DSClassStudent.FirstOrDefault(row => row.sid == u.sid);
            if (s == null)
            {
                return new
                {
                    success = false,
                    msg = "Student不存在"
                };
            }
            var cd = sql.CourseDesign.FirstOrDefault(row => row.username == username);
            var clsStr = sql.DSClass.FirstOrDefault(row => row.cid == s.cid).title;
            if (cd == null)
            {
                cd = new Sql.CourseDesign
                {
                    username = username,
                    subjects = subjects,
                    submitTime = DateTime.Now
                };
                sql.CourseDesign.InsertOnSubmit(cd);
                sql.SubmitChanges();
            }
            var path = string.Format("{0}\\CourseDesign\\{1}\\{2}\\", ConfigurationManager.AppSettings["SafePath"], clsStr, s.sid);
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
            path = string.Format("{0}{1}", path, userfile.FileName);
            userfile.SaveAs(path);
            return new
            {
                success = true
            };
        }
    }
}
